🎖️GitЯра🎖️
core/testing/README.md feat/batch-nodeinfos (a74d5d47) Text, 4.53 KB
/*
• Copyright (c) 2026 Meshtastic LLC
*
• This program is free software: you can redistribute it and/or modify
• it under the terms of the GNU General Public License as published by
• the Free Software Foundation, either version 3 of the License, or
• (at your option) any later version.
*
• This program is distributed in the hope that it will be useful,
• but WITHOUT ANY WARRANTY; without even the implied warranty of
• MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
• GNU General Public License for more details.
*
• You should have received a copy of the GNU General Public License
• along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
T383838:core:testing
Module dependency graph
<!--region graph-->
T282828
Te6edf3graph Te6edf3TB
:Te6edf3coreTb4b4b4:Te6edf3testingTff7b72[Te6edf3testingTff7b72]Tff7b72:::Te6edf3kmpTff7b72-Te6edf3library
Te6edf3classDef Te6edf3androidTff7b72-Te6edf3application Te6edf3fillTb4b4b4:Te6edf3#CAFFBFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3androidTff7b72-Te6edf3applicationTff7b72-Te6edf3compose Te6edf3fillTb4b4b4:Te6edf3#CAFFBFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3composeTff7b72-Te6edf3desktopTff7b72-Te6edf3application Te6edf3fillTb4b4b4:Te6edf3#CAFFBFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3androidTff7b72-Te6edf3feature Te6edf3fillTb4b4b4:Te6edf3#FFD6A5Tb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3androidTff7b72-Te6edf3library Te6edf3fillTb4b4b4:Te6edf3#9BF6FFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3androidTff7b72-Te6edf3libraryTff7b72-Te6edf3compose Te6edf3fillTb4b4b4:Te6edf3#9BF6FFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3androidTff7b72-Te6edf3test Te6edf3fillTb4b4b4:Te6edf3#A0C4FFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3jvmTff7b72-Te6edf3library Te6edf3fillTb4b4b4:Te6edf3#BDB2FFTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3kmpTff7b72-Te6edf3feature Te6edf3fillTb4b4b4:Te6edf3#FFD6A5Tb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3kmpTff7b72-Te6edf3libraryTff7b72-Te6edf3compose Te6edf3fillTb4b4b4:Te6edf3#FFC1CCTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Te6edf3kmpTff7b72-Te6edf3library Te6edf3fillTb4b4b4:Te6edf3#FFC1CCTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
Te6edf3classDef Tff7b72unknown Te6edf3fillTb4b4b4:Te6edf3#FFADADTb4b4b4,Te6edf3strokeTb4b4b4:Te6edf3#000Tb4b4b4,Te6edf3strokeTff7b72-Te6edf3widthTb4b4b4:T79c0ff2Te6edf3pxTb4b4b4,Te6edf3colorTb4b4b4:Te6edf3#000Tb4b4b4;
<!--endregion-->
Overview
The T383838:core:testing module is a dedicated Kotlin Multiplatform (KMP) library that provides shared test fakes, doubles, rules, and utilities. It is designed to be consumed by the T383838commonTest source sets of all other KMP modules to ensure consistent and unified testing behavior across the codebase.
By centralizing fakes and mocking utilities here, we prevent duplication of test setups and enforce a standard approach to testing ViewModels, Repositories, and pure domain logic.
Handling Platform-Specific Setup (Robolectric)
Some KMP modules interact with Android framework components (e.g., T383838android.net.Uri, T383838androidx.room, T383838DataStore) that require Robolectric to run on the JVM. To maintain a unified test suite while providing platform-specific initialization, follow the Subclassing Pattern:
1. Create an Abstract Base Test in T383838commonTest
Place your test logic in an abstract class in T383838src/commonTest. Do NOT use T383838@BeforeTest for setup that requires platform-specific context.
T282828
Tff7b72abstract Tff7b72class T56d364CommonMyViewModelTest Tb4b4b4{
Tff7b72protected Tff7b72lateinit Tff7b72var Te6edf3viewModelTb4b4b4: Te6edf3MyViewModel
T8b949e// Call this from subclasses
Tff7b72fun Td2a8ffsetupRepoTb4b4b4(Tb4b4b4) Tb4b4b4{
T8b949e// ... common setup logic
Tb4b4b4}
Tf0883e@Test
Tff7b72fun Td2a8fftestLogicTb4b4b4(Tb4b4b4) Tb4b4b4{ T8b949e/* ... */ Tb4b4b4}
Tb4b4b4}
2. Implement the JVM Subclass in T383838jvmTest
A simple subclass is usually enough for pure JVM targets.
T282828
Tff7b72class T56d364MyViewModelTest Tb4b4b4: Te6edf3CommonMyViewModelTestTb4b4b4(Tb4b4b4) Tb4b4b4{
Tf0883e@BeforeTest
Tff7b72fun Td2a8ffsetupTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3setupRepoTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
3. Implement the Android Subclass in T383838androidHostTest
Use T383838@RunWith(RobolectricTestRunner::class) and call T383838setupTestContext() to initialize T383838ContextServices.app.
T282828
Tf0883e@RunWithTb4b4b4(Te6edf3RobolectricTestRunnerTff7b72::Te6edf3classTb4b4b4)
Tf0883e@ConfigTb4b4b4(Te6edf3sdk Tff7b72= Tff7b72[T79c0ff3T79c0ff4Tff7b72]Tb4b4b4)
Tff7b72class T56d364MyViewModelTest Tb4b4b4: Te6edf3CommonMyViewModelTestTb4b4b4(Tb4b4b4) Tb4b4b4{
Tf0883e@BeforeTest
Tff7b72fun Td2a8ffsetupTb4b4b4(Tb4b4b4) Tb4b4b4{
Te6edf3setupTestContextTb4b4b4(Tb4b4b4) T8b949e// From :core:testing, initializes Robolectric context
Te6edf3setupRepoTb4b4b4(Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Key Components
• Test Doubles / Fakes: Provides in-memory implementations of core repositories (e.g., T383838FakeNodeRepository, T383838FakeMeshLogRepository) to isolate components under test.
• Coroutines Testing: Provides dispatchers and test rules that replace the main dispatcher with T383838TestDispatcher to allow time-control and synchronous execution of coroutines in tests.
• Mokkery Support: Integrated with the Mokkery compiler plugin to provide robust and unified mocking capabilities in T383838commonTest.
Usage
Add this module to your T383838commonTest source set dependencies in your KMP module's T383838build.gradle.kts:
T282828
Te6edf3kotlin Tb4b4b4{
Te6edf3sourceSets Tb4b4b4{
Te6edf3commonTestTb4b4b4.Te6edf3dependencies Tb4b4b4{
Te6edf3implementationTb4b4b4(Te6edf3projectsTb4b4b4.Te6edf3coreTb4b4b4.Te6edf3testingTb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}
Served by rngit 1.5.0 - Generated in 0.05s